package com.tms.quartz.job;

import com.tms.domain.Tool;
import com.tms.quartz.service.QuartzService;
import com.tms.service.ToolService;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 循环任务
 *
 * @author jackie
 * @date 2020年6月30日
 */
@Component
public class SimpleJob extends QuartzJobBean {

    @Autowired
    QuartzService quartzService;

    @Autowired
    ToolService toolService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {

        JobDataMap dataMap = jobExecutionContext.getMergedJobDataMap();

        int toolId = Integer.parseInt(dataMap.getString("toolId"));

        Tool tool = toolService.findToolById(toolId);

        Map<String, String> map = new HashMap<>(2);
        map.put("toolId", String.valueOf(tool.getId()));

        try {
            quartzService.addJobToTrigger(tool, 2, 1, map);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }
}
